package com.example.demo;


import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/user")
@RestController
public class LoginDemo {
    /**
     * 周次煜大帅哥
     */
    String user = new String("jxtdmn");
    String passNum = "123456";

//    确定是post 请求
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public boolean login(@RequestParam("userName") String userName
            , @RequestParam("password") String password,  HttpSession session) {
//        判断是否合法
        if(userName == null || password == null || "".equals(password) || "".equals(userName)) {
            return false;
        }

//        判断是否正确
        if(user.equals(userName) && passNum.equals(password)) {

//            一旦用户名和密码成立就设置 session
            session.setAttribute("userName", userName);
            return true;
        }

        return false;
    }



//    get请求
    @GetMapping("getLoginUser")
    public  String getLoginUser(HttpSession session) {
//        如果存在用户名就返回
        if(session.getAttribute("userName") != null) {
            return String.valueOf(session.getAttribute("userName"));
        }
//        否则就返回空字符串
        return "";
    }
}
